import 'package:flutter/material.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_message.dart';

class CurrentMessageProvider with ChangeNotifier {
  ///消息缓存
  Map<String, List<V2TimMessage>> _messageMap = new Map();

  Map<String, List<V2TimMessage>> get messageMap => _messageMap;

  ///是否存在该会话信息
  bool containsMessageKey(String key) {
    return _messageMap.containsKey(key);
  }

  ///获取历史记录
  List<V2TimMessage> getHistoryMessage(String key) {
    if (containsMessageKey(key)) {
      return _messageMap[key]!;
    } else {
      return List.empty(growable: true);
    }
  }

  ///添加消息
  addMessage(String key, V2TimMessage message) {
    //判断是否存在这个用户的聊天记录、如果存在则直接添加消息
    if (containsMessageKey(key)) {
      //存在这个聊天
      _messageMap[key]!.add(message);
    } else {
      List<V2TimMessage> _list = List.empty(growable: true);
      _list.add(message);
      _messageMap[key] = _list;
    }
    //排序
    _messageMap[key]!
        .sort((left, right) => left.timestamp!.compareTo(right.timestamp!));
    notifyListeners();

    List<V2TimMessage> list = List.empty(growable: true);
    list.add(message);
    addMessages(key, list);
  }

  Map<String?, V2TimMessage> rebuildMap = new Map<String, V2TimMessage>();

  ///添加消息
  addMessages(String key, List<V2TimMessage>? message) {
    if (message == null || message.isEmpty) {
      return;
    }
    if (containsMessageKey(key)) {
      List<V2TimMessage>? values = _messageMap[key];
      values!.addAll(message);

      //以空间换时间、去掉重复的消息数据
      rebuildMap.clear();
      values.forEach((element) {
        rebuildMap[element.msgID] = element;
      });
      //设置去重后的数据
      _messageMap[key] = rebuildMap.values.toList();
    } else {
      _messageMap[key] = message;
    }
    //排序
    _messageMap[key]!
        .sort((left, right) => left.timestamp!.compareTo(right.timestamp!));
    notifyListeners();
  }

  ///发送消息成功 ,替换消息
  void addOneMessageIfNotExits(String key, V2TimMessage message) {
    print("addOneMessageIfNotExits->$key");
    if (_messageMap.containsKey(key)) {
      bool hasMessage =
          _messageMap[key]!.any((element) => element.msgID == message.msgID);

      print("addOneMessageIfNotExits->1->$hasMessage -->${message.toJson()}");
      if (hasMessage) {
        int idx = _messageMap[key]!
            .indexWhere((element) => element.msgID == message.msgID);
        _messageMap[key]![idx] = message;
      } else {
        _messageMap[key]!.add(message);
      }
    } else {
      List<V2TimMessage> _list = List.empty(growable: true);
      _list.add(message);
      _messageMap[key] = _list;
    }
    //排序
    _messageMap[key]!
        .sort((left, right) => left.timestamp!.compareTo(right.timestamp!));
    notifyListeners();
  }

  ///设置消息已读
  updateC2CMessageByUserId(String userid) {
    String key = "c2c_$userid";
    if (_messageMap.containsKey(key)) {
      List<V2TimMessage> msgList = _messageMap[key]!;
      msgList.forEach((element) {
        element.isPeerRead = true;
      });
      _messageMap[key] = msgList;
      notifyListeners();
    } else {
      print("会话列表不存在这个userid key");
    }
  }
}
